package com.shujia.hdfs;

import org.apache.hadoop.util.hash.Hash;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class ComStudnetScore {
    public static void main(String[] args) {

        /**
         * 读取hdfs数据
         *
         * 1、统计每个学生的总分
         * 2、统计每个班级每个科目的平均分
         *
         * select  clazz,cource ,avg(score) from  s  group by clazz,cource
         *
         */
        ArrayList<Student> students = FileRead.read("/data/student", Student.class);
        ArrayList<Score> scores = FileRead.read("/data/score", Score.class);
        ArrayList<Cource> cources = FileRead.read("/data/cource", Cource.class);

        //存结果集合
        HashMap<String, Integer> map = new HashMap<String, Integer>();

        for (Score score : scores) {
            Integer integer = map.get(score.getS_id());
            if (integer == null) {
                map.put(score.getS_id(), score.getScore());
            } else {
                map.put(score.getS_id(), score.getScore() + integer);
            }
        }

        System.out.println(map);


        /**
         *
         * 2、统计每个班级每个科目的平均分
         *
         * 1、三表关联
         *      需要将学生和科目表转换成map集合
         *      循环分数表，去学生中取班级，去科目表中取科目名
         */

        HashMap<String, Student> studentHashMap = new HashMap<>();
        for (Student student : students) {
            studentHashMap.put(student.getId(), student);
        }

        HashMap<String, Cource> courceHashMap = new HashMap<>();
        for (Cource cource : cources) {
            courceHashMap.put(cource.getId(), cource);
        }

        //以班级和科目名一起作为key   存班级科目总分
        HashMap<String, Integer> sumMap = new HashMap<>();

        //以班级和科目名一起作为key   存班级科目的人数
        HashMap<String, Integer> numMap = new HashMap<>();

        for (Score score : scores) {

            //通过学号取学生班级
            Student student = studentHashMap.get(score.getS_id());
            String clazz = student.getClazz();

            //通过科目编号取科目名
            Cource cource = courceHashMap.get(score.getC_id());
            String name = cource.getName();

            Integer score1 = score.getScore();


            String key = clazz + "-" + name;

            //统计班级科目的总分
            Integer integer = sumMap.get(key);
            if (integer == null) {
                sumMap.put(key, score1);
            } else {
                sumMap.put(key, integer + score1);
            }

            //统计班级科目人数
            Integer integer1 = numMap.get(key);
            if (integer1 == null) {
                numMap.put(key, 1);
            } else {
                numMap.put(key, integer1 + 1);
            }
        }


        //计算平均分

        for (Map.Entry<String, Integer> entry : sumMap.entrySet()) {
            String key = entry.getKey();
            Integer sumScore = entry.getValue();


            //取出班级耳目的人数
            Integer num = numMap.get(key);

            //平均分
            double avg = sumScore / Double.parseDouble(num.toString());

            System.out.println(key + "-" + avg);

        }


    }
}
